$(function () {
    var loginForm = $('.login-form');
    var loginTip = $('.login-tip');
    var regeForm = $('.rege-form');
    var regeTip = $('.rege-tip');
    var colorBox = $('.login-color');
    $('#rege-button').click(function () {
        loginForm.slideUp(400);
        loginTip.slideDown(800);
        regeForm.slideDown(800)
        regeTip.slideUp(400);
        colorBox.animate({
            right: 350
        });
    });
    $('#login-button').click(function () {
        loginTip.slideUp(400);
        loginForm.slideDown(800);
        regeTip.slideDown(800)
        regeForm.slideUp(400);
        colorBox.animate({
            right: 0
        });
    });
});

// 用户注册
$(function () {
    var regPhoneInput = $('#reg-phone');
    var regPasswordInput = $('#reg-password');
    var regConfirmInput = $('#reg-confirm');
    $('#register').click(function () {
        var userPhone = regPhoneInput.val();
        if (!checkPhone(userPhone)) {
            return;
        }
        var userPassword = regPasswordInput.val();
        if (!checkPassword(userPassword)) {
            return;
        }
        var confirmPwd = regConfirmInput.val();
        if (userPassword !== confirmPwd) {
            alert('两次输入的密码不一致');
            return;
        }

        $.ajax({
            url: '/blog/api/register',
            method: 'POST',
            data: {
                phone: userPhone,
                password: userPassword,
            },
            success: function (result) {
                if (result === null || result === undefined) {
                    alert('请求失败');
                    return;
                }
                console.log(result);
                if (result.code !== 200) {
                    alert(result.msg);
                } else {
                    alert(result.msg);
                    regPhoneInput.val('');
                    regPasswordInput.val('');
                    regConfirmInput.val('');
                }
            },
            error: function () {
                alert('请求出错');
            }
        });
    });
});

// 用户登录
$(function () {
    var loginPhoneInput = $('#login-phone');
    var loginPasswordInput = $('#login-password');
    $('#login').click(function () {
        var userPhone = loginPhoneInput.val();
        var userPassword = loginPasswordInput.val();
        if (!checkPhone(userPhone)) {
            return;
        }
        if (!checkPassword(userPassword)) {
            return;
        }
        $.ajax({
            url: '/blog/api/login',
            method: 'POST',
            data: {
                phone: userPhone,
                password: userPassword,
            },
            success: function (result) {
                if (result === null || result === undefined) {
                    alert('请求失败');
                    return;
                }
                if (result.code !== 200) {
                    alert(result.msg);
                } else {
                    alert(result.msg);
                    window.location.href = '/blog/index';
                }
            },
            error: function () {
                alert('请求出错');
            }
        });
    });
});

function checkPhone(phone) {
    if (phone === null || phone === undefined) {
        alert('手机号不能为空');
        return false;
    }
    if (phone.length !== 11) {
        alert('手机号不正确');
        return false;
    }
    return true;
}

function checkPassword(pwd) {
    if (pwd === null || pwd === undefined) {
        alert('登录密码不能为空');
        return false;
    }
    if (pwd.length < 6 || pwd.length > 20) {
        alert('登录密码不正确');
        return false;
    }
    return true;
}